package org.footito.android.data;

import java.io.File;
import java.util.concurrent.RejectedExecutionException;

import org.footito.android.R;
import org.footito.android.data.update.LoadAsyncTask;
import org.footito.android.data.update.LoadAsyncTask.LoadingListener;
import org.footito.android.tools.Log;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;

public class Team implements Comparable<Team>, LoadingListener {
	private String name;
	private String pictureLink;
	private String link;
	private long id;
	private Bitmap picture;
	private TeamGroup group;
	private boolean isLoading = false;

	public static final String PICTURE_FOLDER = Environment.getExternalStorageDirectory()
			+ File.separator + "footito" + File.separator + "pictures" + File.separator;

	/**
	 * @param name
	 * @param picture
	 * @param link
	 * @param id
	 */
	public Team(String name, String pictureLink, String link, long id, TeamGroup group) {
		super();
		this.name = name;
		this.pictureLink = pictureLink;
		this.link = link;
		this.id = id;
		this.group = group;
	}

	public TeamGroup getGroup() {
		return group;
	}

	/**
	 * @return the id
	 */
	public long getId() {
		return id;
	}

	/**
	 * @return the link
	 */
	public String getLink() {
		return link;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @return the picture
	 */
	public Bitmap getPicture(Context context) {
		if (picture == null) {
			String targetPath = Team.PICTURE_FOLDER;
			String targetName = id + LoadAsyncTask.EXTENSION;
			picture = BitmapFactory.decodeFile(targetPath + targetName);
		}

		if (picture == null) {
			loadPicture(context);
			return BitmapFactory.decodeResource(context.getResources(), R.drawable.blason_loading);
		} else {
			return picture;
		}
	}

	/**
	 * @return the pictureLink
	 */
	public String getPictureLink() {
		return pictureLink;
	}

	public void loadPicture(Context context) {
		if (!isLoading) {
			isLoading = true;
			String targetPath = Team.PICTURE_FOLDER;
			StringBuilder targetNameB = new StringBuilder();
			targetNameB.append(id);
			targetNameB.append(LoadAsyncTask.EXTENSION);

			StringBuilder pathNameB = new StringBuilder();
			pathNameB.append(targetPath);
			pathNameB.append(id);
			pathNameB.append(LoadAsyncTask.EXTENSION);

			Bitmap bitmap = BitmapFactory.decodeFile(pathNameB.toString());
			if (bitmap == null) {
				// FIXME Use ThreadPool
				// LoadAsyncTask taskLoadPicture = new LoadAsyncTask(targetPath,
				// targetNameB.toString(), this, this);
				// try {
				// taskLoadPicture.execute((Void[]) null);
				// } catch (RejectedExecutionException e) {
				// Log.w("Exception: " + "RejectedExecutionException: " +
				// e.getMessage());
				// }
			} else {
				this.setPicture(bitmap);
			}
		}
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(int id) {
		this.id = id;
	}

	/**
	 * @param link
	 *            the link to set
	 */
	public void setLink(String link) {
		this.link = link;
	}

	/**
	 * @param name
	 *            the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @param picture
	 *            the picture to set
	 */
	public void setPicture(Bitmap picture) {
		this.picture = picture;
	}

	/**
	 * @param pictureLink
	 *            the pictureLink to set
	 */
	public void setPictureLink(String pictureLink) {
		this.pictureLink = pictureLink;
	}

	@Override
	public int compareTo(Team another) {
		if (this == another)
			return 0;
		if (another == null)
			return 1;
		if (this.getGroup().compareTo(another.getGroup()) == 0) {
			if (id == another.id)
				return 0;
			if (id < another.id)
				return -1;
			else
				return 1;
		} else
			return this.getGroup().compareTo(another.getGroup());
	}

	@Override
	public void onComplete() {
		isLoading = false;
	}
}
